#!/usr/bin/env python
#-*- encoding:utf-8 -*-
import re
import BaseHTTPServer


HOST_NAME = "0.0.0.0" # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 8080 # Maybe set this to 9000.


class MinHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_HEAD(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        
    def do_GET(self):
        """Respond to a GET request."""
        print self.path

        match = re.findall("/([a-z]+)/(\d+)", self.path) 
        if not match:
            self.send_response(403)
            self.send_header("Content-type", "text/html")
            self.end_headers()
        else:
            (cmd, id) = match[0]
            if cmd == "reload":
                msg = self.reload(id)
            elif cmd == "start":
                msg = self.start(id)
            elif cmd == "stop":
                msg = self.stop(id)
            else:
                msg = "error command"
            self.send_response(200)
            self.send_header("Content-type", "text/html")
            self.end_headers()
            self.wfile.write(msg)
    
    def reload(self, id):
        return  "%s, ok" %id
    
    def start(self, id):
        return  "%s, ok" %id
    
    def stop(self, id):
        return  "%s, ok" %id

if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MinHandler)
    print "start http server"
    httpd.serve_forever()

      